using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NUnit.Framework;
using Scribble.Services;
using Scribble.Web.Controllers;
using System.Linq;

namespace Scribble.Tests.Web.Controllers
{
    [TestFixture]
    public class PostControllerTest
    {
        private IPostService postService;
        private PostController postController;

        [Test]
        public void IndexReturnsListOfPosts()
        {
            var result = (ViewResult) postController.Index();

            Assert.That(result.ViewData.Model, Is.InstanceOf<IEnumerable<Post>>());
        }

        [Test]
        public void GetReturnsPostWithCorrectMetadata()
        {
            const int year = 2012;
            const int month = 6;
            const string urlTitle = "my-post-about-things";

            var result = (ViewResult) postController.Get(year, month, urlTitle);

            var post = (Post)result.ViewData.Model;
            Assert.That(post.PublishDate.Month, Is.EqualTo(month));
            Assert.That(post.PublishDate.Year, Is.EqualTo(year));
            Assert.That(post.UrlTitle, Is.EqualTo(urlTitle));
        }

        [SetUp] 
        public void Setup()
        {
            postService = new PostServiceDouble();
            postController = new PostController(postService, null, null);
        }
    }

    public class PostServiceDouble : IPostService
    {
        private readonly IList<Post> posts = new List<Post>
                                                       {
                                                           new Post
                                                               {
                                                                   Title = "A Post",
                                                                   UrlTitle = "a-post",
                                                                   Body = "Here is a post about something.",
                                                                   PublishDate = new DateTime(1990, 10, 25),
                                                                   Summary = "A post about things."
                                                               },
                                                           new Post
                                                               {
                                                                   Title = "Another Post",
                                                                   UrlTitle = "another-post",
                                                                   Body = "Here is another post about something.",
                                                                   PublishDate = new DateTime(1991, 11, 26),
                                                                   Summary = "Another post about other things."
                                                               },
                                                           new Post
                                                               {
                                                                   Title = "My post about things",
                                                                   UrlTitle = "my-post-about-things",
                                                                   Body = "A post about things.",
                                                                   PublishDate = new DateTime(2012, 6, 1),
                                                                   Summary = "A post about things"
                                                               }
                                                       };
        public IList<Post> GetPosts()
        {
            return posts;
        }

        public IList<Post> GetPostsByTag(Tag tag)
        {
            return posts;
        }

        public IList<Post> GetPostsByCategory(Category category)
        {
            return posts;
        }

        public Post GetPost(int year, int month, string urlTitle)
        {
            return posts.First(
                p => p.UrlTitle == urlTitle &&
                     p.PublishDate.Month == month &&
                     p.PublishDate.Year == year);
        }
    }
}